package hiho.ku._1039;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		for (int i = 0; i < t; i++)
			run(in);
		in.close();
	}
	
	private static void run(Scanner in) {
		int ans = 0;
		String s = in.next();
		for (int i = 0; i <= s.length(); i++) {
			ans = Math.max(ans, count(s.substring(0, i) + 'A' + s.substring(i, s.length())));
			ans = Math.max(ans, count(s.substring(0, i) + 'B' + s.substring(i, s.length())));
			ans = Math.max(ans, count(s.substring(0, i) + 'C' + s.substring(i, s.length())));
		}
		System.out.println(ans);
	}
	
	private static int count(String s) {
		s = s + '#';
		int ans = 0;
		boolean finished = false;
		while (!finished) {
			finished = true;
			StringBuilder sb = new StringBuilder();
			int count = 1;
			for (int i = 1; i < s.length(); i++)
				if (s.charAt(i) == s.charAt(i - 1))
					count++;
				else {
					if (count <= 1)
						sb.append(s.charAt(i - 1));
					else {
						ans += count;
						finished = false;
					}
					count = 1;
				}
			sb.append('#');
			s = sb.toString();
		}
		return ans;
	}
	
}
